package com.tedu.parkinglot.order.service.impl;


import com.tedu.parkinglot.base.constants.OrderConstant;
import com.tedu.parkinglot.carLog.pojo.entity.CarLog;
import com.tedu.parkinglot.order.mapper.OrderMapper;
import com.tedu.parkinglot.order.pojo.dto.ChargeParam;
import com.tedu.parkinglot.order.pojo.dto.OrderSaveParam;
import com.tedu.parkinglot.order.pojo.entity.Order;
import com.tedu.parkinglot.order.pojo.vo.OrderVO;
import com.tedu.parkinglot.order.service.OrderService;
import com.tedu.parkinglot.payRecord.mapper.PayRecordMapper;
import com.tedu.parkinglot.payRecord.pojo.entity.PayRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private PayRecordMapper payRecordMapper;

    @Override
    public OrderVO selectByOrderId(Long orderId) {
        OrderVO orderVO = orderMapper.getOrderByOrderId(orderId);
        return orderVO;
    }

//    @Override
//    public String selectByParkRecordId(Long id) {
//
//        CarLog payStatus = orderMapper.getOrderByParkRecordId(id);
//        if(payStatus!= null){
//            return payStatus.getPayStatus();
//        }
//        return "未支付";
//    }

    @Override
    public void saveOrder(OrderSaveParam orderSaveParam) {
        log.debug("orderSaveParam={}", orderSaveParam);
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveParam, order);
        log.debug("order={}", order);
        if (order.getId() == null ) {
            order.setPayStatus(OrderConstant.PAY_STATUS_UNPAID);
            //随机生成订单号
            order.setOrderNum(String.valueOf((System.currentTimeMillis() + (long) (Math.random() * 1000000000))));
            orderMapper.insert(order);
        } else {
            orderMapper.update(order);
        }
    }

    @Override
    public void updateOrderAndPayment(ChargeParam chargeParam) {

        // 保存支付记录
        //支付方式
        if (chargeParam.getPaymentMethod() == null) {
            throw new IllegalArgumentException("支付方式不能为空");
        }
        //订单号
        PayRecord payRecord = new PayRecord();
        payRecord.setPaymentMethod(chargeParam.getPaymentMethod());
        payRecord.setOrderId(chargeParam.getOrderId());
        payRecord.setAmount(chargeParam.getAmount());
        payRecord.setAmountUnit("RMB");
        payRecord.setPayTime(new Date());
        payRecord.setPayNum(String.valueOf((System.currentTimeMillis() + (long) (Math.random() * 1000000000))));
        payRecord.setCreateTime(new Date());
        payRecord.setUpdateTime(new Date());
        payRecord.setUserId(null);
        payRecordMapper.insert(payRecord);

        OrderSaveParam orderSaveParam = new OrderSaveParam();
        orderSaveParam.setId(chargeParam.getOrderId());
        orderSaveParam.setPayStatus(OrderConstant.PAY_STATUS_PAID);
        saveOrder(orderSaveParam);
    }
}
